<template>
  <PanerGrid @onSubmit="handleSubmit" @onReset="handleReset">
    <template v-slot:search>
      <div class="d-group-items d-layout-grid d-layout-grid-4">
        <div class="d-grid-item">
          <div class="d-form-group d-form-group-flex">
            <div class="d-form-label d-text-right">
              <label class="label-text">环境名称</label>
            </div>
            <div class="d-form-el">
              <input v-model="search.code" @keyup.enter="handleSubmit" class="d-form-input" type="search" />
            </div>
          </div>
        </div>
        <div class="d-grid-item">
          <div class="d-form-group d-form-group-flex">
            <div class="d-form-label d-text-right">
              <label class="label-text">编码</label>
            </div>
            <div class="d-form-el">
              <input v-model="search.key" @keyup.enter="handleSubmit" class="d-form-input" type="search" />
            </div>
          </div>
        </div>
        <div class="d-grid-item">
          <FormCalendar
            v-model="search.createTime"
            label="创建时间"
            labelWidth="80px"
            class="d-form-group-flex"
          ></FormCalendar>
        </div>
        <div class="d-grid-item">
          <FormCalendar
            v-model="search.updateTime"
            label="修改时间"
            labelWidth="80px"
            class="d-form-group-flex"
          ></FormCalendar>
        </div>
        <div v-if="false" class="d-grid-item">
          <div class="d-form-group d-form-group-flex">
            <div class="d-form-label d-text-right">
              <label class="label-text">创建人</label>
            </div>
            <div class="d-form-el">
              <input class="d-form-input" />
            </div>
          </div>
        </div>
        <div v-if="false" class="d-grid-item">
          <div class="d-form-group d-form-group-flex">
            <div class="d-form-label d-text-right">
              <label class="label-text">修改人</label>
            </div>
            <div class="d-form-el">
              <input class="d-form-input" />
            </div>
          </div>
        </div>
      </div>
    </template>
    <template v-slot:header>
      <div class="d-paner-caption">
        <span class="d-caption-text">环境列表</span>
        <div class="d-bar-tools d-bar-tools-condensed">
          <span @click="handleAddItem" class="d-tools-item"><a class="d-btn d-btn-plain-sign">添加</a></span>
          <span @click="handleDelChecked" class="d-tools-item"><button class="d-btn d-btn-plain-sign" type="button">删除</button></span>
        </div>
      </div>
      <div class="d-bar-tools d-bar-tools-right d-bar-tools-condensed">
        <a @click="handleReload" class="d-tools-item"><span class="d-btn d-btn-plain-sign d-btn-square-sm d-btn-flex"><RefreshIcon /></span></a>
      </div>
    </template>
    <template v-slot:body>
      <Grid v-model="list" ref="gridList" :request="gridRequest" :isPagerAble="false" :treeProps="{idKey: 'id', idKey: 'pid', childrenKey: 'children'}">
        <GridColumn :width="32" type="tree" />
        <GridColumn :width="48" type="checkbox" />
        <GridColumn :width="60" type="index" label="序号" />
        <GridColumn field="code" label="环境名称"></GridColumn>
        <GridColumn field="key" :width="200" label="编码"></GridColumn>
        <GridColumn field="description" :width="200" label="描述"></GridColumn>
        <GridColumn field="updateTime" label="修改时间" format="datetime|yyyy-MM-dd HH:mm:ss" width="160" />
        <!-- <GridColumn field="updateUser" label="修改人" /> -->
        <GridColumn field="createTime" label="创建时间" format="datetime|yyyy-MM-dd HH:mm:ss" width="160" />
        <!-- <GridColumn field="createUser" label="创建人" /> -->
        <GridColumn field="action" :width="120" label="操作">
          <template v-slot:body="scope">
            <div class="d-bar-tools d-bar-tools-condensed d-grid-actions">
              <a @click="handleEditItem(scope)" class="d-tools-item d-text-theme"><EditIcon size="sm" /></a>
              <a @click="handleAddChild(scope)" class="d-tools-item d-text-theme"><PlusRoundIcon size="sm" /></a>
              <a @click="handleDelItem(scope)" class="d-tools-item d-text-theme"><DeleteIcon size="sm" /></a>
            </div>
          </template>
        </GridColumn>
      </Grid>
      <Edit @onSubmitSuccess="handleEditSuccess" ref="editView" />
    </template>
  </PanerGrid>
</template>

<script>
import { PanerGrid } from '@daelui/vdog/dist/pages'
import { storeMemory } from '@daelui/dogjs/dist/components'
import { treer } from '@daelui/dogjs/dist/components'
import { Grid, GridColumn, Dialoger, FormCalendar, Pager, FormSelecter } from '@daelui/vdog/dist/components'
import { zh as locale } from '@daelui/dogjs/dist/locale'
import { RefreshIcon, EditIcon, DeleteIcon, PlusRoundIcon } from '@daelui/vdog/dist/icons'
import dictionarySer, { api } from '../../../service/action/dictionary.action'
import Edit from './edit.vue'

export default {
  components: {
    PanerGrid,
    Grid,
    GridColumn,
    RefreshIcon,
    EditIcon,
    DeleteIcon,
    PlusRoundIcon,
    FormCalendar,
    FormSelecter,
    Pager,
    Edit
  },

  data () {
    return {
      // 搜索条件
      search: {
        code: '',
        key: '',
        createTime: '',
        updateTime: ''
      },
      // 参数存储key
      paramsKey: 'nave-envirment',
      list: [],
      // 表格请求配置
      gridRequest: {
        url: api.queryList.url, // 请求地址
        method: api.queryList.method, // 请求方式
        params: {cat: 'nave-envirment'}, // 请求参数
        // 请求参数
        onAfterLoad (list) {
          list = Array.isArray(list) ? list : []
          let treeList = treer.arrayToTree(list)
          return treeList
        }
      }
    }
  },

  methods: {
    /**
     * @function 查询事件处理
    */
    handleSubmit () {
      this.$refs.gridList.reload(this.search)
      // 存储条件
      storeMemory.setItem(this.paramsKey, this.search)
    },

    /**
     * @function 重置事件
    */
    handleReset () {
      this.search = {
        code: '',
        key: '',
        createTime: '',
        updateTime: ''
      }
    },
    /**
     * @function 删除选定的数据
    */
    handleDelChecked () {
      let items = this.$refs.gridList.getChecked()
      this.removeItems(items)
    },

    /**
     * @function 删除单条数据
    */
    handleDelItem (item) {
      this.removeItems([item])
    },

    /**
     * @function 删除多条数据
    */
    removeItems (items) {
      items = Array.isArray(items) ? items : []
      if (items.length < 1) {
        Dialoger.warn(locale.list.selectOne)
        return false
      }
      Dialoger.confirm(locale.message.confirmDelete).then(b => {
        if (!b) {
          return false
        }
        items = items.map(item => item.id)
        dictionarySer.removeItem(items).then(result => {
          this.$refs.gridList.reload()
        })
      })
    },

    /**
     * @function 显示与隐藏编辑视图
    */
    showEditView () {
      this.$refs.editView.show(this.editInfo)
    },

    /**
     * @function 添加
    */
    handleAddItem () {
      this.editInfo = {}
      this.showEditView()
    },

    /**
     * @function 添加子节点
    */
    handleAddChild (item) {
      this.editInfo = {
        item: item,
        parent: true
      }
      this.showEditView()
    },

    /**
     * @function 编辑
    */
    handleEditItem (item) {
      this.editInfo = {edit: true, item}
      this.showEditView()
    },

    /**
     * @function 编辑成功
    */
    handleEditSuccess () {
      this.handleReload()
    },

    /**
     * @function 重新加载
    */
    handleReload () {
      this.$refs.gridList.reload()
    }
  },

  mounted () {

  }
}
</script>

<style scoped>
.d-form-label {
  width: 80px;
}
</style>
